/*
 * BarsCanvas.java
 *
 * Created on 18 de noviembre de 2005, 05:09 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */
package com.nextel.project.reporte.mvc;

import com.nextel.project.Configuracion;

import com.nextel.project.pedido.ControlPedido;
import com.nextel.ui.OComponent;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
//import javax.microedition.lcdui.Font;
import com.nextel.util.lwuit.Font;
import javax.microedition.lcdui.Graphics;
import pe.com.nextel.util.math.MathUtil;

/**
 *
 * @author marizaga
 */
public class BarsCanvas extends OComponent{ //Canvas implements CommandListener  {

    private String title = null;
    private int totalCustomers = 0;
    private int totalCustomersAttended = 0;
    private String labelTotalCustomers = null;
    private String labelAttendedCustomers = null;
    private Font font;// = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    private int height = 60;
    private int width = 85;
    private int x = 25;
    private int y = 20;
    private int x1 = x + 10;
    private int y1 = y + 11;
    private int x2 = x + 40;
    private int y2 = y1;
    private int xValues = x + 90;
    private int y1StringValue = y + height + 10;
    private int y2StringValue = y1StringValue + 10;
    private int maxHeightBar = height - 11;
    private int heightSecondBar = 0;
    private int fontColor = 0;//
    private int c1 = 0xFF0000;
    private int c2 = 0x00FF00;
    //private Command cmdSi;
    //private Command cmdNo;

    /** Creates a new instance of BarsCanvas */
    public BarsCanvas(Font textFont ) {
        font =textFont;
//        cmdSi = new Command("", Command.SCREEN, 1);
//        cmdNo = new Command(Configuracion.BOTON_ATRAS, Command.SCREEN, 2);
//
//        this.addCommand(cmdSi);
//        this.addCommand(cmdNo);

//        this.setCommandListener((CommandListener) this);

    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void paint(Graphics g) {
      //System.out.println("paint");

        g.setColor(fontColor);
        //g.setFont(font);

        //  System.out.println("1");
        com.nextel.util.lwuit.Graphics grap = new com.nextel.util.lwuit.Graphics(g);
        grap.setFont(font);
        
        if (getTitle() != null) {
            g.setGrayScale(180);

            g.fillRoundRect((getWidth() / 2) - (font.stringWidth(getTitle()) / 2) - 2, 0, font.stringWidth(getTitle()) + 4, font.getHeight(), 5, 5);

            g.setColor(fontColor);

            g.drawRoundRect((getWidth() / 2) - (font.stringWidth(getTitle()) / 2) - 2, 0, font.stringWidth(getTitle()) + 4, font.getHeight(), 5, 5);

            grap.drawString(getTitle(), getWidth() / 2, 2, Graphics.TOP | Graphics.HCENTER);
        }
        // System.out.println("2");

        g.drawLine(x, y, x, y + height);
        g.drawLine(x, y + height, x + width, y + height);

        g.fillTriangle(x - 2, y + 4, x, y, x + 2, y + 4);
        g.fillTriangle(x + width - 4, y + height - 2, x + width, y + height, x + width - 4, y + height + 2);


        if (totalCustomers > 0) {
            g.setColor(c1);
            g.fillRect(x1, y1, 20, maxHeightBar);//barra
        }

        //  System.out.println("3");

        if (getLabelTotalCustomers() != null) {
            g.setColor(c1);
            g.fillRect(x - 9, y + height + 13, 7, 7);//cuadrado de leyenda
            g.setColor(fontColor);

            //leyenda 1
            grap.drawString(getLabelTotalCustomers(), x, y1StringValue, Graphics.TOP | Graphics.LEFT);

            //valor de leyenda 1
            grap.drawString(String.valueOf(totalCustomers), xValues, y1StringValue, Graphics.TOP | Graphics.RIGHT);
        }

        //  System.out.println("3");

        if (totalCustomersAttended > 0) {
            g.setColor(c2);
            g.fillRect(x2, y2, 20, heightSecondBar);
        }

        // System.out.println("4");

        if (getLabelAttendedCustomers() != null) {
            g.setColor(c2);
            g.fillRect(x - 9, y + height + 23, 7, 7);

            g.setColor(fontColor);
            grap.drawString(getLabelAttendedCustomers(), x, y2StringValue, Graphics.TOP | Graphics.LEFT);

            //valor de leyenda 2
            grap.drawString(String.valueOf(totalCustomersAttended), xValues, y2StringValue, Graphics.TOP | Graphics.RIGHT);
        }

    // System.out.println("5");

    }

    public String getTitle() {
        return title;
    }

    public String getLabelTotalCustomers() {
        return labelTotalCustomers;
    }

    public void setTotalCustomers(int totalCustomers, String labelTotalCustomers) {
        this.totalCustomers = totalCustomers;
        this.labelTotalCustomers = labelTotalCustomers;

        computeRatios();
    }

    public String getLabelAttendedCustomers() {
        return labelAttendedCustomers;
    }

    public void setTotalCustomersAttended(int totalCustomersAttended, String labelAttendedCustomers) {
        this.totalCustomersAttended = totalCustomersAttended;
        this.labelAttendedCustomers = labelAttendedCustomers;

        computeRatios();
    }

    private void computeRatios() {
        //   System.out.println("computeRatios");
        if (totalCustomers > 0 && totalCustomersAttended > 0) {
            String result = MathUtil.dividir(String.valueOf(totalCustomersAttended), String.valueOf(totalCustomers));
            String result2 = MathUtil.setScale(MathUtil.multiplicar(String.valueOf(maxHeightBar), result), 0);
            //result2.setPrecision(0);
            heightSecondBar = Integer.parseInt(result2);

            y2 = y1 + (maxHeightBar - heightSecondBar);
        }
    }

    public int getTotalCustomers() {
        return totalCustomers;
    }

    public int getTotalCustomersAttended() {
        return totalCustomersAttended;
    }

//    public void commandAction(Command c, Displayable d) {

//        if (c == cmdNo) {
//            ControlPedido.getInstance().setCurrent(ControlPedido.getInstance().mvcReporteKPI);
//        }


//    }

    public void destroy() {
        // this.destroy();
    }

    public int getHeight(){
        return height;
    }

    public int getWidth(){
        return width;
    }
}


